package server.gui;

import java.util.ArrayList;
import java.util.Iterator;

import server.ServerMonitor;

/**
 * <h1>Class OnlineUsers</h1>
 *
 * Displays information about the users who are online.
 *
 * @author Tomas
 * @date March 9 2009
 */
public class OnlineUsers extends javax.swing.JFrame {

    /**
	 * Required.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
     * Initializes the window which display the users.
     */
    public OnlineUsers() {
        initComponents();
        this.setLocationRelativeTo(null);
        fillOnlineUsers();
    }

    private void initComponents() {

        onlinePanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        onlineList = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Parlanchin - Online Users");
        setResizable(false);

        onlinePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Online Users"));

        jScrollPane1.setViewportView(onlineList);

        javax.swing.GroupLayout onlinePanelLayout = new javax.swing.GroupLayout(onlinePanel);
        onlinePanel.setLayout(onlinePanelLayout);
        onlinePanelLayout.setHorizontalGroup(
            onlinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(onlinePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
                .addContainerGap())
        );
        onlinePanelLayout.setVerticalGroup(
            onlinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(onlinePanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(onlinePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(onlinePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }

    private void fillOnlineUsers(){
    	
    	ServerMonitor server = ServerMonitor.getInstance();
    	Iterator<String> it = server.getUsersOnline();
    	ArrayList<String> usernames = new ArrayList<String>();
    	
    	while(it.hasNext()){
    		usernames.add(it.next());
    	}
    	
    	String[] names = new String[usernames.size()];
    	usernames.toArray(names);
    	final String[] content = names;
		onlineList.setModel(new javax.swing.AbstractListModel() {
			private static final long serialVersionUID = 1L;
			
			String[] strings = content;
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
    }
    
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList onlineList;
    private javax.swing.JPanel onlinePanel;

}
